package com.spring.cloud.controller;

import com.alibaba.fastjson.JSON;
import com.spring.cloud.entity.User;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/4/2.
 * 测试 http 8种不同请求类型
 */
@RestController
public class HttpController {

    private static Logger logger = LoggerFactory.getLogger(HttpController.class);


    @ApiOperation(value = "http请求之 options 测试")
    @RequestMapping(value = "/options", method = RequestMethod.OPTIONS)
    public String options() {
        logger.info("options test method !");
        return "options";
    }

    @ApiOperation(value = "http请求之 head 测试")
    @RequestMapping(value = "/head", method = RequestMethod.HEAD)
    public String head() {
        logger.info("head test method !");
        return "head";
    }

    @ApiOperation(value = "http请求之 get 测试")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get(HttpServletRequest request) {
        logger.info(request.toString());
        logger.info("get test method !");
        return "get";
    }

    @ApiOperation(value = "http请求之 post 测试")
    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public String post(@RequestBody User user) {
        logger.info("post test method !");
        logger.info("request body : " + JSON.toJSONString(user));
        return "post";
    }

    @ApiOperation(value = "http请求之 put 测试")
    @RequestMapping(value = "/put", method = RequestMethod.PUT)
    public String put(@RequestBody User user) {
        logger.info("put test method !");
        logger.info("request body : " + JSON.toJSONString(user));
        return "put";
    }

    @ApiOperation(value = "http请求之 patch 测试")
    @RequestMapping(value = "/patch", method = RequestMethod.PATCH)
    public String patch(@RequestBody User user) {
        logger.info("patch test method !");
        logger.info("request body : " + JSON.toJSONString(user));
        return "patch";
    }

    @ApiOperation(value = "http请求之 delete 测试")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public String delete(@RequestParam("id") long id) {
        logger.info("delete test method !");
        logger.info("delete id : " + id);
        return "delete";
    }

    @ApiOperation(value = "http请求之 trace 测试")
    @RequestMapping(value = "/trace", method = RequestMethod.TRACE)
    public String trace() {
        logger.info("trace test method !");
        return "trace";
    }


}
